const app = new getApp();
var time = require("../../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    vg_id: '', //核销商品ID
    verification: {}, //核销商品信息
    person_info: {}, //核销员信息
    virtualFlag: 0,
    is_login: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let vg_id = options.id;

    if (options.scene != undefined && options.scene != ''){
      vg_id = options.scene;
    }else{
      that.setData({
        is_login: 1
      })
    }

    that.setData({
      vg_id: vg_id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let is_login = that.data.is_login;

    if (is_login == 0){
      let second = 0;
      let timer = setInterval(function(){

        second++;
        let token = app.globalData.token;
        if(token != ''){
          app.defaultImg(that, true);
          clearInterval(timer);
        }

        if (second >= 30){
          app.showBox('登录超时', 2000);
          setTimeout(function(){
            wx.switchTab({
              url: '/pages/index/index',
            })
          },2000)
        }
      },1000);
    }else{
      app.defaultImg(that, true);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 图片加载失败
   */
  errorImg: function (e) {
    let that = this;
    let verification = that.data.verification;
    let default_img = that.data.default_img;
    let base = that.data.Base;
    let parm = {};
    let img = verification.picture.pic_cover_small;

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_goods_img;
      if (img.indexOf(default_img) == -1) {
        let parm_key = "verification.picture.pic_cover_small";

        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 获取核销商品信息
   */
  loadInfo: function () {
    let that = this;
    let vg_id = that.data.vg_id;
    app.sendRequest({
      url: 'System.Order.verificationExamine',
      data: {
        vg_id: vg_id
      },
      success: function (res) {
        if (res.code == 0) {
          let verification = app.checkEmpty(res.data, {});

          verification.time = '不限制';
          verification.end_time = app.checkEmpty(verification.end_time, '');
          if ( verification.end_time != '') {
            verification.time = verification.end_time == 0 ? verification.time : '到' + time.formatTime(verification.end_time, 'Y-M-D') + '之前有效';
          }
          let img = verification.picture.pic_cover_small;
          verification.picture.pic_cover_small = app.IMG(img);

          that.setData({
            verification: verification,
            is_login: 1,
          })
          if (app.checkEmpty(verification.virtual_code, '') != '') {
            that.loadPersonInfo();
          }
        }
        console.log(res)
      }
    })
  },

  loadPersonInfo: function () {
    let that = this;
    app.sendRequest({
      url: 'System.Order.getVerificationPersonnelInfo',
      success: function (res) {
        if (res.code == 0) {
          let person_info = app.checkEmpty(res.data, {});

          that.setData({
            person_info: person_info,
            is_login: 1,
          })
        }
        console.log(res)
      }
    })
  },

  /**
   * 核销
   */
  verificationVirtualGoods: function(e) {
    let that = this;
    let virtualFlag = that.data.virtualFlag;
    let virtual_goods_id = that.data.vg_id;

    if (virtualFlag == 1){
      return false;
    }
    app.clicked(that, 'virtualFlag');
    
    app.sendRequest({
      url: 'System.Order.verificationVirtualGoods',
      data: {
        virtual_goods_id: virtual_goods_id
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        console.log(res);

        if(code == 0){
          if(data > 0){
            app.showBox(that, "核销成功");
            setTimeout(function(e){
              wx.switchTab({
                url: '/pages/member/member/member',
              })
            },1500);
          }else{
            app.showBox(that, '核销失败');
            that.loadInfo();
          }
        }
      }
    })
  }
})